package com.ce.pms.custservice.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.custservice.service.IDFormManageService;
import com.ce.pms.custservice.vo.DispatchFormVO;
import com.ce.pms.custservice.vo.FeedbackRecordVO;
import com.ce.pms.custservice.vo.FeedbackSearchVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

public class FeedbackRecordAction extends PmsBaseAction {

	/**
	 * 功能描述:<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-17<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateFeedbackRecordMain(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId){
		return "feedbackRecordMain";
	}

	/**
	 * 功能描述:<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-17<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String findFeedbackRecordPage(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchFormId,
			@Read(key="MenuFlat",defaultValue="true")@Out(key="MenuFlat")String MenuFlat,
			@Read(key = "ec_crd",defaultValue="10")int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){
		this.getRequest().setAttribute("pageSize", new Integer(pageSize));
		FeedbackSearchVO feedbackSearchVO = new FeedbackSearchVO();
		feedbackSearchVO.setDispatchFormId(dispatchFormId);
		feedbackSearchVO.setPageNo(pageNo);
		feedbackSearchVO.setPageSize(pageSize);
		feedbackSearchVO.setSortStr(ExtremeUtils.getOrderString(this.getRequest()));
		Page page=dispatchFormManageService.queryFeedbackRecordByPage(feedbackSearchVO);

		List<FeedbackRecordVO> list = page.getResult();
		Long totalRows = page.getTotalCount();
		this.getRequest().setAttribute("backRecordList", list);
		this.getRequest().setAttribute("totalRows", totalRows.intValue());


//		Page page = new Page();
//		List taskList =initBackRecordList();
//		page.setResult(taskList);
//		setAttribute("backRecordList", taskList);
//
//		setAttribute("totalRows", new Integer(taskList.size()));
		return "feedbackRecordList";
	}

	/**
	 * 功能描述:造数据用的,service弄好了,就删除<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-11  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List initBackRecordList(){
		List<FeedbackRecordVO> backRecordList = new ArrayList<FeedbackRecordVO>();

		for(int i = 0; i < 10; i++){
			FeedbackRecordVO feedbackRecordVO =new FeedbackRecordVO();
			feedbackRecordVO.setKeyword(Long.parseLong(""+i));
			feedbackRecordVO.setDispatchNo(""+i);
			feedbackRecordVO.setRecordTime(new Date());
			backRecordList.add(feedbackRecordVO);
		}
		return backRecordList;
	}

	/**
	 * 功能描述:导航到反馈新增页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-17<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateFeedbackRecordAdd(@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchId){
		DispatchFormVO dispatchFormVO=dispatchFormManageService.getDFormByID(dispatchId);
		FeedbackRecordVO vo=new FeedbackRecordVO();
        vo.setDispatchNo(dispatchFormVO.getDispatchNo());
        vo.setEndTime(dispatchFormVO.getEndTime());
        vo.setStartTime(dispatchFormVO.getStartTime());
        vo.setRecorder(this.getUsername());
        vo.setRecordTime(new Date());

		this.getRequest().setAttribute("feedbackRecordVO", vo);

		return "feedbackRecordAdd";
	}

	/**
	 * 功能描述:新增派工单反馈<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-17<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String addFeedbackRecord(@Read FeedbackRecordVO feedbackRecordVO,
			@Read(key="startTime")String startTime,
			@Read(key="endTime")String endTime,
			@Read(key="recordTime")String recordTime){
		feedbackRecordVO.setStartTime(DateUtil.convertStrToDate(startTime,"yyyy-MM-dd HH:mm"));
		feedbackRecordVO.setEndTime(DateUtil.convertStrToDate(endTime,"yyyy-MM-dd HH:mm"));
		feedbackRecordVO.setRecordTime(DateUtil.convertStrToDate(recordTime,"yyyy-MM-dd HH:mm"));
		feedbackRecordVO.setCreator(this.getUserId());
		dispatchFormManageService.addFeedback(feedbackRecordVO.getDispatchFormId(), feedbackRecordVO);
		this.getRequest().setAttribute("iframeName", "backRecordList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述:作废反馈<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-17<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String disannulFeedbackRecord(@Read(key="feedbackRecordId")Long feedbackRecordId,
			@Read(key="dispatchFormId")Long dispatchFormId,@Read(key="remarkInfo")String remarkInfo){
		FeedbackRecordVO feedbackRecordVO = new FeedbackRecordVO();
		feedbackRecordVO.setRemark(remarkInfo);
		feedbackRecordVO.setKeyword(feedbackRecordId);
		feedbackRecordVO.setCreator(this.getUserId());
		feedbackRecordVO.setDispatchFormId(dispatchFormId);
		dispatchFormManageService.setAsideFeedback(feedbackRecordVO);
		this.getRequest().setAttribute("iframeName", "backRecordList");
		return "onlyRefresh";
	}

	/**
	 * 功能描述:导航到作废反馈页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-17<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateFeedbackRecordDisannul(@Read(key="feedbackRecordId")@Out(key="feedbackRecordId")Long feedbackRecordId,
			@Read(key="dispatchFormId")@Out(key="dispatchFormId")Long dispatchId){
		return "feedbackRecordDisannul";
	}

	/**
	 * 功能描述:导航到反馈详细页面<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2008-12-17<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String navigateFeedbackRecordDetail(@Read(key="feedbackRecordId")Long feedbackRecordId){

		FeedbackRecordVO vo=dispatchFormManageService.getFeedbackRecordById(feedbackRecordId);
		if(BlankUtil.isBlank(vo)){
			vo=new FeedbackRecordVO();
		}

		this.getRequest().setAttribute("feedbackRecordVO", vo);
		return "feedbackRecordDetail";
	}


	private IDFormManageService dispatchFormManageService;




	public void setDispatchFormManageService(IDFormManageService dispatchFormManageService) {
		this.dispatchFormManageService = dispatchFormManageService;
	}



	/**
	 * 功能描述:检查指定的派工单是否允许增加反馈<br>
	 * @param <br>
	 * @return<br>
	 *
	 * Create author:xuhaidui<br>
	 * Create on:2009-1-9<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void checkIsAllowAddFeedback(@Read(key="dispatchFormId")Long dispatchFormId){
		dispatchFormManageService.checkIsAllowAddFeedback(dispatchFormId);
	}

	public void checkIsAllowSetAsideFeedback(@Read(key="dispatchFormId")Long dispatchFormId,
			@Read(key="feedbackId")Long feedbackId){
		dispatchFormManageService.checkIsAllowSetAsideFeedback(dispatchFormId,feedbackId);
	}

}
